package com.wdh.kjs.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.wdh.kjs.R;
import com.wdh.kjs.activity.InvestmentDetailActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by feifei on 2016/4/20.
 */

public class TuijianAdapter extends BaseAdapter {

    private List<HashMap<String, String>> data=new ArrayList<HashMap<String,String>>();
    private Context context;
    private HashMap<String, String> maps=new HashMap<String, String>();
    public TuijianAdapter(Context context,List<HashMap<String, String>> data) {
        this.data=data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.shouyetuijian_v2, null);
            viewHolder.tuijian_text = (TextView) convertView.findViewById(R.id.tuijian_text);
//            viewHolder.tuijian_text1 = (TextView) convertView.findViewById(R.id.tuijian_text1);
            viewHolder.tuijian_text2 = (TextView) convertView.findViewById(R.id.tuijian_text2);
            viewHolder.tuijian_text3 = (TextView) convertView.findViewById(R.id.tuijian_text3);
            viewHolder.tuijian_text4 = (TextView) convertView.findViewById(R.id.tuijian_text4);
            viewHolder.tuijian_text5 = (TextView) convertView.findViewById(R.id.tuijian_text5);
            viewHolder.tuijian_availableBalance = (TextView) convertView.findViewById(R.id.tuijian_availableBalance);
            viewHolder.tuijian_text6 = (LinearLayout) convertView.findViewById(R.id.tuijian_text6);
            viewHolder.shouye_linear = (LinearLayout) convertView.findViewById(R.id.shouye_linear);
            viewHolder.tv_mifs_progressbar = (ProgressBar) convertView.findViewById(R.id.tv_mifs_progressbar);
            viewHolder.shouye_percent = (TextView) convertView.findViewById(R.id.shouye_percent);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        maps  = data.get(position);

        viewHolder.tuijian_text.setText(maps.get("style"));
//        viewHolder.tuijian_text1.setText(maps.get("name"));
        viewHolder.tuijian_text2.setText(maps.get("money"));
        viewHolder.tuijian_text3.setText(maps.get("per"));
        viewHolder.tuijian_text4.setText(maps.get("day"));
        viewHolder.tuijian_text5.setText(maps.get("total"));

        switch (maps.get("item_investment_state")){
            case "抢购":viewHolder.tuijian_text6.setBackgroundResource(R.drawable.touzhiclick);
                viewHolder.tuijian_text2.setVisibility(View.VISIBLE);
                viewHolder.tuijian_availableBalance.setVisibility(View.VISIBLE);
                break;
            case "预热中":viewHolder.tuijian_text6.setBackgroundResource(R.drawable.yure);
                viewHolder.tuijian_text2.setVisibility(View.INVISIBLE);
                viewHolder.tuijian_availableBalance.setVisibility(View.INVISIBLE);
                break;
            case "还款完成":viewHolder.tuijian_text6.setBackgroundResource(R.drawable.huankuan);
                viewHolder.tuijian_text2.setVisibility(View.INVISIBLE);
                viewHolder.tuijian_availableBalance.setVisibility(View.INVISIBLE);
                break;
            case "还款中":viewHolder.tuijian_text6.setBackgroundResource(R.drawable.huankuanzhong);
                viewHolder.tuijian_text2.setVisibility(View.INVISIBLE);
                viewHolder.tuijian_availableBalance.setVisibility(View.INVISIBLE);
                break;
            case "募集成功":viewHolder.tuijian_text6.setBackgroundResource(R.drawable.shouqin);
                viewHolder.tuijian_text2.setVisibility(View.INVISIBLE);
                viewHolder.tuijian_availableBalance.setVisibility(View.INVISIBLE);
                break;
            case "坏账":viewHolder.tuijian_text6.setBackgroundResource(R.mipmap.ic_launcher);
                viewHolder.tuijian_text2.setVisibility(View.INVISIBLE);
                viewHolder.tuijian_availableBalance.setVisibility(View.INVISIBLE);
                break;
            default:break;
        }

//        viewHolder.tuijian_text6.setText(maps.get("per1"));
        double a = Double.parseDouble(maps.get("percent"));
        int i= (int) a;
        viewHolder.tv_mifs_progressbar.setProgress(i);
        viewHolder.shouye_percent.setText(maps.get("percent") + "%");
        viewHolder.shouye_linear.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        String status = data.get(position).get("status");
        String state=data.get(position).get("item_investment_state");

        Intent intent = new Intent(context, InvestmentDetailActivity.class);
        intent.putExtra("itemId", data.get(position).get("itemId"));
        intent.putExtra("status", status+"");
        intent.putExtra("item_investment_state", state);
        context.startActivity(intent);
    }
});
        return convertView;
    }
    class ViewHolder{
        TextView tuijian_text;
        TextView tuijian_text2;
        TextView tuijian_text3;
        TextView tuijian_text4;
        TextView tuijian_text5;
        TextView tuijian_availableBalance;
        LinearLayout tuijian_text6;
        ProgressBar tv_mifs_progressbar;
        TextView shouye_percent;
        LinearLayout shouye_linear;

    }
}